from pydantic import BaseModel
from typing import Optional
from datetime import datetime

class UserCreate(BaseModel):
    username: str
    password: str

class UserLogin(BaseModel):
    username: str
    password: str

class UserUpdate(BaseModel):
    username: Optional[str] = None
    password: Optional[str] = None

class UserOut(BaseModel):
    id: int
    username: str
    created_at: Optional[str] = None
    created_at: datetime  # ✅ 改成 datetime

    class Config:
        from_attributes = True
        
    @classmethod
    def from_orm(cls, obj):
        """自定义from_orm方法处理datetime转换"""
        data = {
            'id': obj.id,
            'username': obj.username,
            'created_at': obj.created_at.isoformat() if obj.created_at else None
        }
        return cls(**data)
